import QtQuick 1.0
import "Models"
import "Delegates"
import "ListViews"
import "WebAccess/HttpAccess.js" as WebAccess
import "WebAccess/TwitterAPI.js" as TwitterAPI
import "Util/Util.js" as Util

Rectangle{
    id: _root
    anchors.left: parent.left
    anchors.top: parent.bottom
    anchors.right: parent.right
    anchors.bottom: parent.bottom

    property int hidePosX: -parent.width

//    property bool loading: _modelTimeline.isReady// !isReady()


//    //全部の状態をまとめる
//    function isReady(){
//        return _modelTimeline.isReady && _modelMention.isReady && _modelDirectMessage.isReady
//            && _modelListLists.isReady && _modelRetweetByMe.isReady && _modelRetweetOfMe.isReady
//            && _modelRetweetToMe.isReady && _modelFriends.isReady && _modelFollowers.isReady
//            && _modelFavorites.isReady;
//    }



    /////////////////////////////////////////////
    //自動更新処理
    Timer {
        interval: _globalSetting.updateInterval * 1000
        running: true
        repeat: true
        onTriggered: {
//            Util.outputDebugString("Timer: triggered")
            update();
        }
    }



    //////////////////////////////////////////////
    //アイテムをクリックしてコンテキストメニュー出したりとかの処理
    function onClickContextItem(type){
//        Util.outputDebugString("onClickContextItem:"
//                               + "type=" + type
//                      + " id=" + _selectItemInfo.itemId
//                      + ",name=" + _selectItemInfo.screenName);

        var next_menu_state = "";

        switch(type){
        case 0:
            //Mention
            if(_selectItemInfo.screenName.length === 0){
            }else if(_tweetContainer.text.indexOf("@" + _selectItemInfo.screenName) >= 0){
                //既にある
            }else{
                _tweetContainer.text = "@" + _selectItemInfo.screenName + " " + _tweetContainer.text;
                _tweetContainer.inReplyToId = _selectItemInfo.itemId;
            }
            break;
        case 1:
            //Retweet
            next_menu_state = "RetweetCaution";
            break;
        case 2:
            //QT
            if(_selectItemInfo.screenName.length === 0){
            }else{
                _tweetContainer.text = " QT @"
                                    + _selectItemInfo.screenName
                                    + ": "
                                    + _selectItemInfo.message;
                _tweetContainer.inReplyToId = _selectItemInfo.itemId;
            }
            break;
        case 3:
            //DM
            if(_selectItemInfo.screenName.length === 0){
            }else{
                _tweetContainer.text = "D " + _selectItemInfo.screenName + " ";
            }
            break;

        case 50:
            //User Info
            _contentContainer.state = "Users";
            break;

        case 51:
            //User Timeline
            _contentContainer.state = "UserTimeline";
            break;

        case 100:
            //Follow
            Util.outputDebugString("Follow");
            TwitterAPI.sendFriendshipRequest(_selectItemInfo.screenName, new onFriendshipRequest());
            break;

        default:
            break;
        }

        //使ったら消す
        if(next_menu_state.length === 0){
            _selectItemInfo.clear();
        }

        return next_menu_state;
    }


    //フォローの応答
    function onFriendshipRequest(text){
        _toast.show("Followed");
    }


    /////////////////////////////////////////////
    //更新する
    function update(){
        Util.outputDebugString("update : state=" + state);
        switch(state){
        case "Timeline":
            _modelTimeline.getRecent();
            break;
        case "Mention":
            _modelMention.getRecent();
            break;
        case "DirectMessage":
            _modelDirectMessage.getRecent();
            break;
        case "ListLists":
            _modelListLists.getRecent();
            break;
        case "ListStatuses":
            _modelListStatuses.getRecent();
            break;
        case "RetweetToMe":
            _modelRetweetToMe.getRecent();
            break;
        case "RetweetByMe":
            _modelRetweetByMe.getRecent();
            break;
        case "RetweetOfMe":
            _modelRetweetOfMe.getRecent();
            break;
        case "Friends":
            _modelFriends.getRecent();
            break;
        case "Followers":
            _modelFollowers.getRecent();
            break;
        case "Favorites":
            _modelFavorites.getRecent();
            break;
        case "Users":
            _modelUsers.getRecent();
            break;
        case "UserTimeline":
            _modelUserTimeline.getRecent();
            break;
        default:
            break;
        }
    }


    ////////////////////////////////////
    //Timeline
    ListViewTimeline {
        id: _listTimeline
        x: hidePosX

        //下へ突きぬけた
        onMoreChanged: {
//            Util.outputDebugString("p more=" + more);
            if(more){
//                _timeline.more();
            }
        }

        //ヘッダー
        header: ListViewFooter {
            text: "Recent..."
            loading: !_modelTimeline.isReady
            onClicked: {
                _modelTimeline.getRecent();
            }
        }

        //フッター
        footer: ListViewFooter {
            text: "More..."
            loading: !_modelTimeline.isReady
            onClicked: {
                _modelTimeline.getMore();
            }
        }

        //モデル
        model: ModelTimeline{
            id: _modelTimeline
            sourceType: 0
        }
    }


    ////////////////////////////////////
    //Mention
    ListViewTimeline {
        id: _listMention
        x: hidePosX

        //ヘッダー
        header: ListViewFooter {
            text: "Recent..."
            loading: !_modelMention.isReady
            onClicked: {
                _modelMention.getRecent();
            }
        }

        //フッター
        footer: ListViewFooter {
            text: "More..."
            loading: !_modelMention.isReady
            onClicked: {
                _modelMention.getMore();
            }
        }

        //モデル
        model: ModelTimeline{
            id: _modelMention
            sourceType: 1
        }
    }

    ////////////////////////////////////
    //DM
    ListViewTimeline {
        id: _listDirectMessage
        x: hidePosX

        //ヘッダー
        header: ListViewFooter {
            text: "Recent..."
            loading: !_modelDirectMessage.isReady
            onClicked: {
                _modelDirectMessage.getRecent();
            }
        }

        //フッター
        footer: ListViewFooter {
            text: "More..."
            loading: !_modelDirectMessage.isReady
            onClicked: {
                _modelDirectMessage.getMore();
            }
        }

        //モデル
        model: ModelTimeline{
            id: _modelDirectMessage
            sourceType: 2
        }
    }



    ////////////////////////////////////
    //List Lists
    ListViewListLists {
        id: _listListLists
        x: hidePosX

        //モデル
        model: ModelListLists{
            id: _modelListLists
        }
    }

    ////////////////////////////////////
    //List statuses
    ListViewTimeline {
        id: _listListStatuses
        x: hidePosX

        //ヘッダー
        header: ListViewFooter {
            text: "Recent..."
            loading: !_modelListStatuses.isReady
            onClicked: {
                _modelListStatuses.getRecent();
            }
        }

        //フッター
        footer: ListViewFooter {
            text: "More..."
            loading: !_modelListStatuses.isReady
            onClicked: {
                _modelListStatuses.getMore();
            }
        }

        //モデル
        model: ModelTimeline{
            id: _modelListStatuses
            listId: _selectItemInfo.listId
            sourceType: 4
        }
    }

    ////////////////////////////////////
    //Retweets
    ListViewRetweets {
        id: _listRetweets
        x: hidePosX

        model: ModelRetweets{
        }
    }



    ////////////////////////////////////
    //Retweet To me
    ListViewTimeline {
        id: _listRetweetToMe
        x: hidePosX


        //ヘッダー
        header: ListViewFooter {
            text: "Recent..."
            loading: !_modelRetweetToMe.isReady
            onClicked: {
                _modelRetweetToMe.getRecent();
            }
        }

        //フッター
        footer: ListViewFooter {
            text: "More..."
            loading: !_modelRetweetToMe.isReady
            onClicked: {
                _modelRetweetToMe.getMore();
            }
        }

        //モデル
        model: ModelTimeline{
            id: _modelRetweetToMe
            sourceType: 7
        }
    }

    ////////////////////////////////////
    //Retweet by me
    ListViewTimeline {
        id: _listRetweetByMe
        x: hidePosX


        //ヘッダー
        header: ListViewFooter {
            text: "Recent..."
            loading: !_modelRetweetByMe.isReady
            onClicked: {
                _modelRetweetByMe.getRecent();
            }
        }

        //フッター
        footer: ListViewFooter {
            text: "More..."
            loading: !_modelRetweetByMe.isReady
            onClicked: {
                _modelRetweetByMe.getMore();
            }
        }

        //モデル
        model: ModelTimeline{
            id: _modelRetweetByMe
            sourceType: 8
        }
    }

    ////////////////////////////////////
    //Retweet of me
    ListViewTimeline {
        id: _listRetweetOfMe
        x: hidePosX


        //ヘッダー
        header: ListViewFooter {
            text: "Recent..."
            loading: !_modelRetweetOfMe.isReady
            onClicked: {
                _modelRetweetOfMe.getRecent();
            }
        }

        //フッター
        footer: ListViewFooter {
            text: "More..."
            loading: !_modelRetweetOfMe.isReady
            onClicked: {
                _modelRetweetOfMe.getMore();
            }
        }

        //モデル
        model: ModelTimeline{
            id: _modelRetweetOfMe
            sourceType: 9
        }
    }


    ////////////////////////////////////
    //Friends
    ListViewUsers {
        id: _listFriends
        x: hidePosX

        //モデル
        model: ModelFriendsFollowers{
            id: _modelFriends
            sourceType: 0
        }
    }

    ////////////////////////////////////
    //Followers
    ListViewUsers {
        id: _listFollowers
        x: hidePosX

        //モデル
        model: ModelFriendsFollowers{
            id: _modelFollowers
            sourceType: 1
        }
    }

    ////////////////////////////////////
    //Favorites
    ListViewTimeline {
        id: _listFavorites
        x: hidePosX


        //ヘッダー
        header: ListViewFooter {
            text: "Recent..."
            loading: !_modelFavorites.isReady
            onClicked: {
                _modelFavorites.getRecent();
            }
        }

        //フッター
        footer: ListViewFooter {
            text: "More..."
            loading: !_modelFavorites.isReady
            onClicked: {
                _modelFavorites.getMore();
            }
        }

        //モデル
        model: ModelTimeline{
            id: _modelFavorites
            sourceType: 10
        }
    }


    ////////////////////////////////////
    // Users
    ListViewUsers {
        id: _listUsers
        x: hidePosX

        //モデル
        model: ModelFriendsFollowers{
            id: _modelUsers
            lastSelectScreenName: _selectItemInfo.screenName
            lastSelectMessage: _selectItemInfo.message
            sourceType: 2
        }
    }

    ////////////////////////////////////
    //UserTImeline
    ListViewTimeline {
        id: _listUserTimeline
        x: hidePosX

        //ヘッダー
        header: ListViewFooter {
            text: "Recent..."
            loading: !_modelUserTimeline.isReady
            onClicked: {
                _modelUserTimeline.getRecent();
            }
        }

        //フッター
        footer: ListViewFooter {
            text: "More..."
            loading: !_modelUserTimeline.isReady
            onClicked: {
                _modelUserTimeline.getMore();
            }
        }

        //モデル
        model: ModelTimeline{
            id: _modelUserTimeline
            listId: _selectItemInfo.screenName
            sourceType: 11
        }
    }

    ////////////////////////////////////
    //Home
    ListViewHome {
        id: _listHome
        x: 0

        model: ModelHome{

        }
    }


    ////////////////////////////////////
    //本文の表示エリアの状態
    states: [
        State {
            name: "Timeline"
            //位置変更
            PropertyChanges{
                target: _listHome
                x: hidePosX
            }
            PropertyChanges{
                target: _listTimeline
                x: 0
            }
            //タイトル表示変更
            PropertyChanges {
                target: _actionBar
                appState: "Timeline"
            }
        },
        State {
            name: "Mention"
            //位置変更
            PropertyChanges{
                target: _listHome
                x: hidePosX
            }
            PropertyChanges{
                target: _listMention
                x: 0
            }
            //タイトル表示変更
            PropertyChanges {
                target: _actionBar
                appState: "Mention"
            }
        },
        State {
            name: "DirectMessage"
            //位置変更
            PropertyChanges{
                target: _listHome
                x: hidePosX
            }
            PropertyChanges {
                target: _listDirectMessage
                x: 0
            }
            //タイトル表示変更
            PropertyChanges {
                target: _actionBar
                appState: "DM"
            }
        },
        State {
            name: "ListLists"
            //位置変更
            PropertyChanges{
                target: _listHome
                x: hidePosX
            }
            PropertyChanges {
                target: _listListLists
                x: 0
            }
            //タイトル表示変更
            PropertyChanges {
                target: _actionBar
                appState: "Lists"
            }
        },
        State {
            name: "ListStatuses"
            //位置変更
            PropertyChanges{
                target: _listHome
                x: hidePosX
            }
            PropertyChanges {
                target: _listListStatuses
                x: 0
            }
            //タイトル表示変更ListStatuses
            PropertyChanges {
                target: _actionBar
                appState: _selectItemInfo.listName
            }
        },
        State {
            name: "Retweets"
            //位置変更
            PropertyChanges{
                target: _listHome
                x: hidePosX
            }
            PropertyChanges {
                target: _listRetweets
                x: 0
            }
            //タイトル表示変更
            PropertyChanges {
                target: _actionBar
                appState: "Retweets"
            }
        },
        State {
            name: "RetweetToMe"
            //位置変更
            PropertyChanges{
                target: _listHome
                x: hidePosX
            }
            PropertyChanges {
                target: _listRetweetToMe
                x: 0
            }
            //タイトル表示変更
            PropertyChanges {
                target: _actionBar
                appState: "Retweets by others"
            }
        },
        State {
            name: "RetweetByMe"
            //位置変更
            PropertyChanges{
                target: _listHome
                x: hidePosX
            }
            PropertyChanges {
                target: _listRetweetByMe
                x: 0
            }
            //タイトル表示変更
            PropertyChanges {
                target: _actionBar
                appState: "Retweets by you"
            }
        },
        State {
            name: "RetweetOfMe"
            //位置変更
            PropertyChanges{
                target: _listHome
                x: hidePosX
            }
            PropertyChanges {
                target: _listRetweetOfMe
                x: 0
            }
            //タイトル表示変更
            PropertyChanges {
                target: _actionBar
                appState: "Retweets of you"
            }
        },
        State {
            name: "Friends"
            //位置変更
            PropertyChanges{
                target: _listHome
                x: hidePosX
            }
            PropertyChanges {
                target: _listFriends
                x: 0
            }
            //タイトル表示変更
            PropertyChanges {
                target: _actionBar
                appState: "Friends"
            }
        },
        State {
            name: "Followers"
            //位置変更
            PropertyChanges{
                target: _listHome
                x: hidePosX
            }
            PropertyChanges {
                target: _listFollowers
                x: 0
            }
            //タイトル表示変更
            PropertyChanges {
                target: _actionBar
                appState: "Followers"
            }
        },
        State {
            name: "Favorites"
            //位置変更
            PropertyChanges{
                target: _listHome
                x: hidePosX
            }
            PropertyChanges{
                target: _listFavorites
                x: 0
            }
            //タイトル表示変更
            PropertyChanges {
                target: _actionBar
                appState: "Favorites"
            }
        },
        State {
            name: "Users"
            //位置変更
            PropertyChanges{
                target: _listHome
                x: hidePosX
            }
            PropertyChanges{
                target: _listUsers
                x: 0
            }
            //タイトル表示変更
            PropertyChanges {
                target: _actionBar
                appState: "Users"
            }
        },
        State {
            name: "UserTimeline"
            //位置変更
            PropertyChanges{
                target: _listHome
                x: hidePosX
            }
            PropertyChanges{
                target: _listUserTimeline
                x: 0
            }
            //タイトル表示変更
            PropertyChanges {
                target: _actionBar
                appState: "UserTimeline"
            }
        }

    ]

    //本文の表示エリアのアニメーション
    transitions:
        Transition {
            SequentialAnimation {
//                NumberAnimation {
//                    easing.type: Easing.OutCubic
//                    target: _listHome
//                    properties: "x"
//                    duration: 500
//                }
                ScriptAction {
                    script: update();
                }

                NumberAnimation {
                    easing.type: Easing.OutCubic
                    targets: [_listHome
                            , _listTimeline, _listMention, _listDirectMessage
                            , _listListLists, _listListStatuses
                            , _listRetweets, _listRetweetToMe, _listRetweetByMe, _listRetweetOfMe
                            , _listFriends, _listFollowers, _listFavorites
                            , _listUsers, _listUserTimeline]
                    property: "x"
                    duration: 500
                }
            }
        }

}
